package com.example.finaltask;

import static com.example.finaltask.utils.Check.checkEmail;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.finaltask.modle.User;
import com.example.finaltask.utils.DBHelper;
import com.example.finaltask.utils.MD5;
import com.example.finaltask.utils.Remove;

import java.io.Serializable;

public class SignUpActivity extends Activity {
    private DBHelper dbhelper;
    private EditText email;
    private EditText password;

    private Button signUpButton;
    private Button backToLogin;
    private ImageView clearEmail;
    private ImageView seePWD;

    private View background;
    private boolean isShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        Remove.removeTop(this);
        //文本框
        email = findViewById(R.id.signUp_email_text);
        password = findViewById(R.id.signUp_password_text);
        //按钮
        signUpButton = findViewById(R.id.signUp_confirm);
        backToLogin = findViewById(R.id.back_to_login);
        clearEmail = findViewById(R.id.clear_email);
        //背景
        background = findViewById(R.id.background);
        //注册表格
        formEditListener();
        //注册
        signUpListener();
        //从登录界面跳过来
//        Intent intent = getIntent();
//        String data = intent.getStringExtra("email");
//        if(data!=null)
//            email.setText(data);
    }

    /**
     *@description 基本信息验证
     */
    public boolean checkInfo(){
        if(!checkEmail(email.getText().toString())){
            Toast.makeText(getBaseContext(),"邮箱格式错误", Toast.LENGTH_LONG).show();
            return false;
        }
        if(password.getText().length()<6){
            Toast.makeText(getBaseContext(),"密码不少于6位", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }
    /**
     *@description 注册监听器
     */
    public void signUpListener(){
        signUpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此处进行判断是否被注册
                if(checkInfo()){
                    User user = new User();
                    user.setEmail(email.getText().toString());
                    //MD5加密
                    user.setPassword(MD5.getMD5String(password.getText().toString()));
                    //用户名默认邮箱,可以修改
                    user.setUserName(email.getText().toString());
                    //默认用户头像
                    user.setHeadImage("https://img.zcool.cn/community/016efd56e823f032f875520f84f543.jpg@1280w_1l_2o_100sh.jpg");
                    DBHelper dbhelper = new DBHelper(SignUpActivity.this,"murmur.db",null,7);
                    SQLiteDatabase db = dbhelper.getWritableDatabase();
                    //创建存放数据的ContentValues对象
                    ContentValues values = new ContentValues();
                    values.put("email",user.getEmail());
                    values.put("headimage",user.getHeadImage());
                    values.put("password",user.getPassword());
                    values.put("username",user.getUserName());
                    //数据库执行插入命令
                    db.insert("user", null, values);
                    Toast.makeText(getBaseContext(),"注册成功", Toast.LENGTH_LONG).show();
                    //跳转登录页面,传递对象
                    Intent intent = new Intent();
                    intent.setClass(SignUpActivity.this,LoginActivity.class);
                    Bundle bundle = new Bundle();
                    //由于插入数据库的密码加密,这边传递要改回来
                    user.setPassword(password.getText().toString());
                    bundle.putSerializable("user",  user);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }
    /**
     *@description 注册的填写
     */
    public void formEditListener(){
        //清除邮箱
        clearEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                email.setText("");
            }
        });
        //密码可见
        seePWD = findViewById(R.id.see_signUp);
        seePWD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果密码可见
                if(isShow){
                    //切换图标
                    seePWD.setBackground(getDrawable(R.drawable.ic_eye_close));
                    //设置密码不可见
                    password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                    //改变状态值为假
                    isShow = false;
                }else{
                    //切换图标
                    seePWD.setBackground(getDrawable(R.drawable.ic_eye));
                    //设置密码可见
                    password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    //改变状态值为真
                    isShow = true;
                }
            }
        });
        //返回登录
        backToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转登录页面
                Intent intent = new Intent();
                intent.setClass(SignUpActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}